﻿/*************************************************************************************
 * 文 件 名:   HK_MultipleChoiceItem
 * 
 * 描    述:   多选题模板类
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2021/11/25
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;

namespace HKTools
{
    public class HK_MultipleChoiceItem : HK_QuesItem
    {
        public Toggle optionA_Toggle;
        Text optionA_Lable;
        public Toggle optionB_Toggle;
        Text optionB_Lable;
        public Toggle optionC_Toggle;
        Text optionC_Lable;
        public Toggle optionD_Toggle;
        Text optionD_Lable;

        List<Toggle> option_List = new List<Toggle>();

        List<GameObject> optionBG_List = new List<GameObject>();

        List<int> answerArray = new List<int>();

        public void InitQuesItem(HK_MultipleChoiceData quesData, int id)
        {
            ID_Txt.text = id.ToString() + "、";

            answerArray = new List<int>();

            List<char> answerCharArray = (quesData.Answer.ToUpper().ToCharArray()).ToList();

            if (answerCharArray.Contains('A'))
                answerArray.Add(0);

            if (answerCharArray.Contains('B'))
                answerArray.Add(1);

            if (answerCharArray.Contains('C'))
                answerArray.Add(2);

            if (answerCharArray.Contains('D'))
                answerArray.Add(3);

            option_List.Add(optionA_Toggle);
            option_List.Add(optionB_Toggle);
            option_List.Add(optionC_Toggle);
            option_List.Add(optionD_Toggle);

            optionA_Toggle.isOn = false;
            optionB_Toggle.isOn = false;
            optionC_Toggle.isOn = false;
            optionD_Toggle.isOn = false;

            for (int i = 0; i < option_List.Count; i++)
            {
                option_List[i].onValueChanged.AddListener(isOn =>
                {
                    isCorrect = true;

                    for (int i = 0; i < option_List.Count; i++)
                    {
                        if (option_List[i].isOn)
                        {
                            if (!answerArray.Contains(i))
                                isCorrect = false;
                        }
                        else
                        {
                            if (answerArray.Contains(i))
                                isCorrect = false;
                        }
                    }
                });
            }

            optionA_Lable = optionA_Toggle.transform.Find("Label").GetComponent<Text>();
            optionB_Lable = optionB_Toggle.transform.Find("Label").GetComponent<Text>();
            optionC_Lable = optionC_Toggle.transform.Find("Label").GetComponent<Text>();
            optionD_Lable = optionD_Toggle.transform.Find("Label").GetComponent<Text>();

            optionBG_List.Add(optionA_Toggle.transform.Find("Background").gameObject);
            optionBG_List.Add(optionB_Toggle.transform.Find("Background").gameObject);
            optionBG_List.Add(optionC_Toggle.transform.Find("Background").gameObject);
            optionBG_List.Add(optionD_Toggle.transform.Find("Background").gameObject);

            quesName_Txt.text = quesData.QuesName;

            optionA_Lable.text = quesData.Options[0];
            optionB_Lable.text = quesData.Options[1];
            optionC_Lable.text = quesData.Options[2];
            optionD_Lable.text = quesData.Options[3];

            analysis_Txt.text = quesData.Analysis;
        }

        public void HideToggle()
        {
            for (int i = 0; i < optionBG_List.Count; i++)
            {
                optionBG_List[i].SetActive(false);
            }
        }

        public override bool IsCorrect()
        {
            return isCorrect;
        }

        public override bool IsDone()
        {
            isDone = false;

            for (int i = 0; i < option_List.Count; i++)
            {
                if (option_List[i].isOn)
                    isDone = true;
            }

            return isDone;
        }
    }
}